package com.css.fxfzfxqh.modules.buildingIndividual.controller;

import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.modules.buildingIndividual.entity.BuildingIndividualEntity;
import com.css.fxfzfxqh.modules.buildingIndividual.service.BuildingIndividualService;
import com.css.fxfzfxqh.modules.buildingIndividual.vo.ParamsVo;
import com.css.fxfzfxqh.util.PlatformPageUtils;
import com.css.fxfzfxqh.util.PlatformSessionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/buildingIndividual")
@Slf4j
public class BuildingIndividualController {

    @Autowired
    BuildingIndividualService buildingIndividualService;


    /**
     * 导入
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public RestResponse upload(@RequestParam("file") MultipartFile file) {
        RestResponse response = null;
        try{
            Map<String, Object> map = buildingIndividualService.upload(file);
            boolean res = ((Boolean) map.get("res")).booleanValue();;
            if(res){
                response = RestResponse.succeed("上传成功!");
                response.setMessage("上传成功!");
            }else {
                response = RestResponse.succeed("上传失败!");
                response.setMessage(map.get("mess").toString());
            }
        }catch (Exception e){
            String errormessage = "上传失败!";
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 分页查询
     * @param paramsVo
     * @param request
     * @return
     */
    @GetMapping("/page")
    public RestResponse page(ParamsVo paramsVo, HttpServletRequest request) {
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> map = buildingIndividualService.page(paramsVo,curPage,pageSize);
            response = RestResponse.succeed(map);
        }catch (Exception e){
            log.error("获取列表信息失败!",e);
            response=RestResponse.fail("获取列表信息失败!");
        }
        return response;
    }

    /**
     * 获取房屋单体版本
     * @return
     */
    @GetMapping("/getVersion")
    public RestResponse getVersion(ParamsVo paramsVo, HttpServletRequest request) {
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> map = buildingIndividualService.getVersion(paramsVo,curPage,pageSize);
            response = RestResponse.succeed(map);
            response.setMessage("查询成功");
        }catch (Exception e){
            log.error("查询失败!",e);
            response=RestResponse.fail("查询失败!");
        }
        return response;
    }
    /**
     * 修改
     * @param buildingIndividualEntity
     * @return
     */
    @PostMapping("/update")
    public RestResponse update(@RequestBody BuildingIndividualEntity buildingIndividualEntity) {
        RestResponse response = null;
        try {
            boolean res = buildingIndividualService.update(buildingIndividualEntity);
            if(res){
                response = RestResponse.succeed("修改成功!");
                response.setMessage("修改成功!");
            }else {
                response = RestResponse.fail("修改失败!");
            }
        } catch (Exception e) {
            String errormessage = "修改失败!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 根据id删除数据
     * @param
     * @return
     */
    @GetMapping("/delete")
    public RestResponse delete(String ids) {
        RestResponse response = null;
        try{
            boolean res = buildingIndividualService.delete(ids);
            if(res){
                response = RestResponse.succeed("删除成功!");
                response.setMessage("删除成功!");
            }else {
                response = RestResponse.fail("删除失败!");
            }
        }catch (Exception e){
            log.error("删除失败!",e);
            response=RestResponse.fail("删除失败!");
        }
        return response;
    }

    /**
     * 查询详情页
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public RestResponse findById(String id) {
        RestResponse response = null;
        try{
            BuildingIndividualEntity buildingIndividualEntity = buildingIndividualService.findById(id);
            response = RestResponse.succeed(buildingIndividualEntity);
            response.setMessage("查询成功");
        }catch (Exception e){
            log.error("查询失败!",e);
            response=RestResponse.fail("查询失败!");
        }
        return response;
    }

    /**
     * 获取当前登录人角色
     * @return
     */
    @GetMapping("/getRole")
    public RestResponse getRole() {
        RestResponse response = null;
        try{
            Map<String, String> map= buildingIndividualService.getRole();
            response = RestResponse.succeed(map);
            response.setMessage("查询成功");
        }catch (Exception e){
            log.error("查询失败!",e);
            response=RestResponse.fail("查询失败!");
        }
        return response;
    }

    /**
     * 地图数据显示
     * @return
     */
    @GetMapping("/queryBuildingMap")
    public RestResponse queryBuildingMap(ParamsVo paramsVo) {
        RestResponse response = null;
        try{
            Map<String,Object> map = buildingIndividualService.queryBuildingMap(paramsVo);
            response = RestResponse.succeed(map);
            response.setMessage("查询成功");
        }catch (Exception e){
            log.error("查询失败!",e);
            response=RestResponse.fail("查询失败!");
        }
        return response;
    }
}
